var connectedPeers = {};
var peer = new Peer('sdfiweinfwefwefwe-foweif_fiefwewjwienve', {
  // Set API key for cloud server (you don't need this if you're running your
  // own.
  key: 'x7fwx2kavpy6tj4i',

  // Set highest debug level (log everything!).
  debug: 3,

  // Set a logging function:
  logFunction: function() {
    var copy = Array.prototype.slice.call(arguments).join(' ');
    $('#logbox').append(copy + '<br>');
  }
});

// Show this peer's ID.
peer.on('open', function(id){
  console.log('peer on open, id: ' + id);
});

peer.on('error', function(err) {
  console.log(err);
})

peer.on('connection', connect);


        var dataView;
        var dataBlob;
        var url;

function connect(c) {
  // Handle a chat connection.
  if (c.label === 'chat') {
    /*var chatbox = $('<div></div>').addClass('connection').addClass('active').attr('id', c.peer);
    var header = $('<h1></h1>').html('Chat with <strong>' + c.peer + '</strong>');
    var messages = $('<div><em>Peer connected.</em></div>').addClass('messages');
    chatbox.append(header);
    chatbox.append(messages);*/

    console.log('on chat connect')
 
    // Select connection handler.
    chatbox.on('click', function() {
      if ($(this).attr('class').indexOf('active') === -1) {
        $(this).addClass('active');
      } else {
        $(this).removeClass('active');
      }
    });
    $('.filler').hide();
    $('#connections').append(chatbox);

    c.on('data', function(data) {
      /*messages.append('<div><span class="peer">' + c.peer + '</span>: ' + data +
        '</div>');*/
        console.log('chat connect on data: ' + data);
        });
    c.on('close', function() {
      /*alert(c.peer + ' has left the chat.');
      chatbox.remove();
      if ($('.connection').length === 0) {
        $('.filler').show();
      }*/
      console.log('chat connect with ' + c.peer + 'closed')
      delete connectedPeers[c.peer];
    });
  } else if (c.label === 'file') {
    console.log('on file connect from ' + c.peer);
    c.on('data', function(data) {
      console.log('on data in connect with ' + c.peer);
      // If we're getting a file, create a URL for it.
      if (data.constructor === ArrayBuffer) {
        dataView = new Uint8Array(data);
        dataBlob = new Blob([dataView]);
        url = window.URL.createObjectURL(dataBlob);
       /* $('#' + c.peer).find('.messages').append('<div><span class="file">' +
            c.peer + ' has sent you a <a target="_blank" href="' + url + '">file</a>.</span></div>');*/
      }
    });
  }
  connectedPeers[c.peer] = 1;
}


var imgFile;
$('#img-input').on('change', function() {
  imgFile = $(this)[0].files;
  imgFile = imgFile[0];
});

var tmpDataUrl;
var fileDataUrlReader = new FileReader();
fileDataUrlReader.onload = function (fileReaderEvent) {
  tmpDataUrl = fileReaderEvent.target.result;
};

function setBlobToImgType(blob) {
  blob = new Blob([blob],{type : 'image/jpeg'})
  return blob;
}

function getDataUrlOfFile(file) {
  fileDataUrlReader.readAsDataURL(file);
  return tmpDataUrl;
}
  function doNothing(e){
    e.preventDefault();
    e.stopPropagation();
  }
$(document).ready(function() {
  console.log('doc ready');
  var box = $('#img-drop-box');
  box.on('dragenter', doNothing);
  box.on('dragover', doNothing);
  box.on('drop', function(e){
    e.originalEvent.preventDefault();
    imgFile = e.originalEvent.dataTransfer.files[0];
    /*eachActiveConnection(function(c, $c) {
      if (c.label === 'file') {
        c.send(file);
        $c.find('.messages').append('<div><span class="file">You sent a file.</span></div>');
      }
    });*/
  });
});